package com.ugrow.ugrowcommunity.vm.schedule;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.databinding.ClassScheduleItemBinding;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.Holder> {

    private Context context;
    private List<ClassBean> list;
    private int parentHeight;
    private int parentWidth;
    private HashMap<String, Integer> map = new HashMap<>();

    public ClassAdapter(Context context, List<ClassBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        parentHeight = parent.getHeight();
        parentWidth = parent.getWidth();
        return new Holder(LayoutInflater.from(context).inflate(R.layout.class_schedule_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.itemView.getLayoutParams().height = parentHeight / 5;
        holder.itemView.getLayoutParams().width = parentWidth / 5;
        for (int i = 0; i < list.size(); i++) {
            Log.d("TAG", "下标：" + position + ": 第" + ((position % 5) + 1) + "周， 第" + ((position / 5) + 1) + "节");
            if (String.valueOf((position % 5) + 1).equals(list.get(i).getDateName()) && list.get(i).getTime() == (position / 5) + 1) {
                holder.itemBinding.classCard.setVisibility(View.VISIBLE);
                holder.itemBinding.CourseName.setText(list.get(i).getCourseName());
                holder.itemBinding.classroom.setText(list.get(i).getClassroomName());
                holder.itemBinding.teacherName.setText(list.get(i).getTeacherName());
                GradientDrawable mGradientDrawable = (GradientDrawable) holder.itemBinding.classCard.getBackground();
                mGradientDrawable.setColor(getRandomColor(list.get(i).getCourseName()));
                return;
            } else {
                holder.itemBinding.classCard.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return 25;
    }

    static class Holder extends RecyclerView.ViewHolder {
        private ClassScheduleItemBinding itemBinding;

        public Holder(@NonNull View itemView) {
            super(itemView);
            itemBinding = ClassScheduleItemBinding.bind(itemView);
        }
    }

    public int getRandomColor(String courseName) {
        Random random = new Random();
        Integer integer = map.get(courseName);
        if (integer == null) {
            int argb = Color.argb(200, random.nextInt(256), random.nextInt(256), random.nextInt(256));
            map.put(courseName, argb);
            return argb;
        } else return integer;
    }
}
